<?php

/**
 * 广告位置管理
 * @author chenshubo
 */
namespace App\Model\PssModel;

use App\Model\BaseModel\BasePssModel;
use App\Service\Code\Normal;
use App\Service\AppService\SystemCacheConfigService;

class AdvertiseManage extends BasePssModel
{
    public $table = 'advertise';
    
    /**
     * 需要展示的广告位map
     * @var array
     */
    public $advertiseMap = [
        '2'     => 'seo_list',
        '3'     => 'bottom_first',
        '4'     => 'bottom_second',
        '5'     => 'bottom_third',
        '6'     => 'bottom_forth',
        '7'     => 'bottom_company_info',
        '8'     => 'top_logo'
    ];
    
    /**
     * 获取广告位信息
     */
    public function getAdvertiseList()
    {
        $adids = array_keys($this->advertiseMap);
        $list = Banner::query()
        ->whereIn('type',$adids)
        ->select([
            'id',
            'type',
            'title',
            'image',
            'link'
        ])
        ->get()
        ->toArray();
        if( empty($list) ) {
            return Normal::returnCode(Normal::FAIL,"广告位数据未配置");
        }
        
        $advertiseMap = [];
        foreach ($list as $item ) {
            $pos = $this->advertiseMap[$item['type']];
            $advertiseMap[$pos][] = $item;
            unset($item);
        }
        
        //取出支付方式
        $advertiseMap['payinfo']  = SystemCacheConfigService::instance()->getCacheInfoBySystemCode('pay_way');
        //取出公司官网配置
        $jump_config = ShopHomeShow::instance()->getHomeShowConfigOneByShowType(ShopHomeShow::JUMP_URL);
        if (!empty($jump_config['status'])){
            $advertiseMap['jump_config'] = $jump_config;
        }else{
            $advertiseMap['jump_config'] = [];
        }

        return Normal::returnCode(Normal::SUC,"获取广告位配置信息成功",$advertiseMap);
    }
    
    /**
     * 获取广告位详情
     * @param array $param
     */
    public function getAdvertiseDetail( $param = [] )
    {
        $body = $param['body'];
        if( empty($body['ad_id']) ) {
            return Normal::returnCode(Normal::FAIL,"except param ad_id");
        }
        
        $row = Banner::query()
        ->where('id',$body['ad_id'])
        ->select([
            'id',
            'type',
            'title',
            'contents',
            'image',
            'link'
        ])
        ->first();
       
        if( empty($row) ) {
            return Normal::returnCode(Normal::FAIL,"当前广告信息不存在");
        }
        
        return Normal::returnCode(Normal::SUC,"获取成功",$row->toArray());
    }
}